package com.bolt.report.business.modules.datasource.controller;

import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.report.business.modules.datasource.dto.DataSetAnalyzeParamDTO;
import com.bolt.report.business.modules.datasource.dto.ReportDataSourceDTO;
import com.bolt.report.business.modules.datasource.dto.ReportDataSourceQueryCriteria;
import com.bolt.report.business.modules.datasource.dto.converter.ReportDataSourceConverter;
import com.bolt.report.business.modules.datasource.entity.ReportDataSourceEntity;
import com.bolt.report.business.modules.datasource.service.ReportDataSourceService;
import com.bolt.support.codegenerator.dto.TableInfo;
import com.bolt.support.codegenerator.model.Column;
import com.bolt.support.codegenerator.model.Table;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2021-11-17 14:15:55
 **/
@RestController
@RequestMapping("report/design/datasource")
@RequiredArgsConstructor
@Api(tags = "数据源配置相关")
public class ReportDataSourceController {

    private final ReportDataSourceService reportDataSourceService;


    @PostMapping({"/findAll"})
    public ResultMessage findAll() {
        List<ReportDataSourceEntity> dataSourceEntities = reportDataSourceService.queryAllDataSource();
        return Results.success(ReportDataSourceConverter.toDTO(dataSourceEntities));
    }


    @GetMapping({"/queryTableName"})
    public ResultMessage queryTableNam(String id, String tableName, Pageable pageable) {
        PageInfo<TableInfo> dbTables = reportDataSourceService.buildDatabaseTables(id, tableName, pageable);
        return Results.success(dbTables.map(k -> ReportDataSourceConverter.toJsonData(k)));
    }

    @PostMapping({"/loadTableData"})
    public ResultMessage loadTableData(@Validated @RequestBody DataSetAnalyzeParamDTO sqlAnalyzeParam, Pageable pageable) {
        List<Map<String, Object>> tableData = reportDataSourceService.loadTableData(sqlAnalyzeParam, pageable);
        return Results.success(tableData);
    }

    @PostMapping({"/loadTableField"})
    public ResultMessage<List<Column>> loadTableData(@RequestBody ReportDataSourceQueryCriteria criteria) {
        Table table = reportDataSourceService.loadTable(criteria.getDataSourceId(), criteria.getTableName());
        return Results.success(table.getColumns());
    }


    @PostMapping({"/testDataSource"})
    public ResultMessage<Void> testDataSource(@Validated @RequestBody ReportDataSourceDTO dao) {
        reportDataSourceService.testConnection(dao);
        return Results.success();
    }

    /**
     * 根据ID操作
     */
    @GetMapping("/get/{id}")
    @ApiOperation(notes = "根据ID查询", httpMethod = "GET", value = "根据ID查询")
    public ResultMessage<ReportDataSourceDTO> get(@PathVariable String id) {
        ReportDataSourceEntity reportDataSourceEntity = reportDataSourceService.findOne(id).get();
        return Results.success(ReportDataSourceConverter.toDTO(reportDataSourceEntity));
    }


    /**
     * 新增操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "新增", httpMethod = "POST", value = "新增")
    public ResultMessage<Void> create(@Validated @RequestBody ReportDataSourceDTO dao) {
        reportDataSourceService.saveOrUpdate(dao);
        return Results.success();
    }


    /**
     * 删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除", httpMethod = "DELETE", value = "根据ID删除")
    public ResultMessage<Void> doDelete(@RequestBody Set<String> ids) {
        for (String id : ids) {
            reportDataSourceService.deleteById(id);
        }
        return Results.success();
    }


}